<?php

/**
 * iq actions.
 *
 * @package    alazhar
 * @subpackage iq
 * @author     Ardhy Widodo
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class iqActions extends sfActions {
    
    public function __construct($context, $moduleName, $actionName) {
        parent::__construct($context, $moduleName, $actionName);
        $this->getUser()->setAttribute('subsystem', 'bk');
    }

    public function executeIndex(sfWebRequest $request) {
        $this->iqs = Doctrine_Core::getTable('Iq')
                ->createQuery('a')
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->getUser()->shutdown();
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new IqForm();
        $this->kecerdasan_list = Doctrine_Core::getTable('Kecerdasan')->findAll();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new IqForm();
        $this->kecerdasan_list = Doctrine_Core::getTable('Kecerdasan')->findAll();
        $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
        if ($this->form->isValid()) {
            $iq = $this->form->save();
            foreach (Doctrine_Core::getTable('Kecerdasan')->findAll() as $value) {
                
            }
            //$this->redirect('iq/new');
        }
        //$this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($iq = Doctrine_Core::getTable('Iq')->find(array($request->getParameter('id'))), sprintf('Object iq does not exist (%s).', $request->getParameter('id')));
        $this->form = new IqForm($iq);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($iq = Doctrine_Core::getTable('Iq')->find(array($request->getParameter('id'))), sprintf('Object iq does not exist (%s).', $request->getParameter('id')));
        $this->form = new IqForm($iq);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($iq = Doctrine_Core::getTable('Iq')->find(array($request->getParameter('id'))), sprintf('Object iq does not exist (%s).', $request->getParameter('id')));
        $iq->delete();

        $this->redirect('iq/index');
    }

    public function executeJsonsiswa(sfWebRequest $request) {
        if ($siswa = Doctrine_Core::getTable('Siswa')->findOneByNoInduk($request->getParameter('no_induk'))) {
            $usia = date('Y') - date_parse($siswa->tanggal_lahir)['year'];
            $data = array('id' => $siswa->id, 'nama' => $siswa->nama_lengkap, 'jk' => $siswa->jk, 'usia' => $usia);
            $data['success'] = true;
        } else {
            $data['success'] = false;
        }

        $json = json_encode($data, true);
        $this->getResponse()->setHttpHeader('Content-type', 'application/json');
        $this->renderText($json);
        return sfView::NONE;
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $iq = $form->save();
            $this->redirect('iq/new');
        }
    }

}
